package com.company.project.web;

import com.company.project.application.service.IRelUserRoleService;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.RelUserRole;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by admin on 2018/08/29.
 */
@RestController
@RequestMapping("/rel/user/role")
@Api(tags = "用户角色关系管理")
public class RelUserRoleController {

    @Autowired
    private IRelUserRoleService IRelUserRoleService;

    @PostMapping
    @ApiOperation(value = "添加单个值", notes = "添加单个值的接口")
    public Result add(@RequestBody RelUserRole relUserRole) {
        IRelUserRoleService.save(relUserRole);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个值", notes = "删除单个值的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "主键id", required = true)
    })
    public Result delete(@PathVariable Integer id) {
        IRelUserRoleService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    @ApiOperation(value = "修改单个值", notes = "修改单个值的接口")
    public Result update(@RequestBody RelUserRole relUserRole) {
        IRelUserRoleService.update(relUserRole);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取单个值", notes = "获取单个值的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "主键id", required = false)
    })
    public Result detail(@PathVariable Integer id) {
        RelUserRole relUserRole = IRelUserRoleService.findById(id);
        return ResultGenerator.genSuccessResult(relUserRole);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "分页获取所有列表", notes = "分页获取所有列表的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "page", value = "分页值", required = false),
            @ApiImplicitParam(paramType = "query", name = "size", value = "分页大小", required = false)
    })
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        PageHelper.startPage(page, size);
        List<RelUserRole> list = IRelUserRoleService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
